package com.sskj.quantization.ui.fragment;


import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.quantization.R;
import com.sskj.quantization.R2;
import com.sskj.quantization.bean.CashBeforeBean;
import com.sskj.quantization.bean.QuanRecordBean;
import com.sskj.quantization.presenter.RecordFragmentPresenter;
import com.sskj.quantization.util.TipQuanUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

@Route(path = RConfig.QUAN_FRAGMENT_RECORD)//量化记录
public class RecordFragment extends BaseFragment<RecordFragmentPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private SlimAdapter slimAdapter;
    private MVCCoolHelper<List<QuanRecordBean>> mvcHelper;

    @Autowired
    boolean isHold;//是否持仓
    private int curPos;
    private BottomSheetDialog cashTip;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.quan_fragment_record;
    }

    @Override
    public RecordFragmentPresenter getPresenter() {
        return new RecordFragmentPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 20))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        // 剩余数量
        slimAdapter = SlimAdapter.create().register(R.layout.quan_recy_item_record, new SlimInjector<QuanRecordBean>() {
            @Override
            public void onInject(QuanRecordBean data, IViewInjector injector, List payloads) {
                injector.text(R.id.tvBuyTime, isHold ? data.getAddtime() : App.INSTANCE.getString(R.string.quan_recordFragment1_h) + data.getAddtime())
                        .text(R.id.tvNum, String.format(App.INSTANCE.getString(R.string.quan_recordFragment2_h), data.getCon_no()))
                        .text(R.id.tvDayNum, String.format(App.INSTANCE.getString(R.string.quan_recordFragment3_h), data.getDay()))
                        .text(R.id.tvUSDT, String.format("%sUSD", data.getMoney()))
                        .text(R.id.tvUSDAccumulate, String.format("%sUSD", data.getProfit()))
                        .text(R.id.tvRate, data.getRate()+"%")
                        .visibility(R.id.btCash, isHold ? View.VISIBLE : View.GONE)
                        .visibility(R.id.tvCashTime, isHold ? View.GONE : View.VISIBLE)
                        .text(R.id.tvCashTime, isHold?"":(App.INSTANCE.getString(R.string.quan_recordFragment4_h) + data.getSelltime()))
                        .clicked(R.id.btCash, view -> {
                            id = data.getId();
                            mPresenter.cashBefore(data.getId());
                        })
                ;
            }
        });
        slimAdapter.attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<QuanRecordBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<QuanRecordBean>)
                page -> mPresenter.quanRecord(isHold ? "1" : "2", page + "", "20"), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    public void showCash(CashBeforeBean cashBeforeBean) {

        cashTip = TipQuanUtil.showCash(getActivity(), cashBeforeBean.getMoney(), cashBeforeBean.getMoney_db(), cashBeforeBean.getFee(), input -> {
            cashTip.dismiss();
            mPresenter.cash(id, input[0]);

        });
        cashTip.show();
    }

    public void cashSuccess() {
        if (mvcHelper!=null){
            mvcHelper.refresh();
        }
    }
}
